import unittest
import requests

class TestIHRMLogin(unittest.TestCase):


    # 登录成功
    def test01_login_success(self):
        resp=requests.post(url="http://ihrm2-test.itheima.net/api/sys/login",
                   json={"mobile":"13800000002","password":"123456"})

        self.assertEqual(200,resp.status_code)
        self.assertEqual(True,resp.json().get("success"))
        self.assertEqual(10000,resp.json().get("code"))
        self.assertIn("操作成功",resp.json().get("message"))

    # 手机号不存在
    def test02_mobile_not_exist(self):
        resp = requests.post(url="http://ihrm2-test.itheima.net/api/sys/login",
                             json={"mobile": "13911000002", "password": "123456"})

        self.assertEqual(200, resp.status_code)
        self.assertEqual(False, resp.json().get("success"))
        self.assertEqual(20001, resp.json().get("code"))
        self.assertIn("用户名或密码错误", resp.json().get("message"))
    # 密码错误
    def test03_pwd_error(self):
        resp = requests.post(url="http://ihrm2-test.itheima.net/api/sys/login",
                             json={"mobile": "13800000002", "password": "12345678"})

        self.assertEqual(200, resp.status_code)
        self.assertEqual(False, resp.json().get("success"))
        self.assertEqual(20001, resp.json().get("code"))
        self.assertIn("用户名或密码错误", resp.json().get("message"))
    # 多参
    def test04_more_params(self):
        resp = requests.post(url="http://ihrm2-test.itheima.net/api/sys/login",
                             json={"mobile": "13800000002", "password": "123456","abc":"123"})

        self.assertEqual(200, resp.status_code)
        self.assertEqual(True, resp.json().get("success"))
        self.assertEqual(10000, resp.json().get("code"))
        self.assertIn("操作成功", resp.json().get("message"))
    # 少参
    def test05_less_params(self):
        resp = requests.post(url="http://ihrm2-test.itheima.net/api/sys/login",
                             json={"mobile": "13800000002"})

        self.assertEqual(200, resp.status_code)
        self.assertEqual(False, resp.json().get("success"))
        self.assertEqual(20001, resp.json().get("code"))
        self.assertIn("用户名或密码错误", resp.json().get("message"))
    # 无参
    def test06_null_params(self):
        resp = requests.post(url="http://ihrm2-test.itheima.net/api/sys/login",
                             json={})

        self.assertEqual(200, resp.status_code)
        self.assertEqual(False, resp.json().get("success"))
        self.assertEqual(20001, resp.json().get("code"))
        self.assertIn("用户名或密码错误", resp.json().get("message"))

    # 参数错误
    def test07_error_params(self):
        resp = requests.post(url="http://ihrm2-test.itheima.net/api/sys/login",
                             json={"phone": "13800000002", "password": "123456"})

        self.assertEqual(200, resp.status_code)
        self.assertEqual(False, resp.json().get("success"))
        self.assertEqual(20001, resp.json().get("code"))
        self.assertIn("用户名或密码错误", resp.json().get("message"))